    /*
     * Copyright 2016-2021 Pnoker. All Rights Reserved.
     * Licensed under the Apache License, Version 2.0 (the "License");
     * you may not use this file except in compliance with the License.
     * You may obtain a copy of the License at
     *     http://www.apache.org/licenses/LICENSE-2.0
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     */

    package com.rundow.driver.virtual.impl;

    import cn.hutool.core.util.RandomUtil;
    import com.rundow.common.base.bean.driver.AttributeInfo;
    import com.rundow.common.base.constant.CommonConstant;
    import com.rundow.common.base.model.Device;
    import com.rundow.common.base.model.Point;
    import com.rundow.common.sdk.bean.driver.ContainerContext;
    import com.rundow.common.sdk.service.DriverCustomService;
    import com.rundow.common.sdk.service.DriverService;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;

    import javax.annotation.Resource;
    import java.util.Map;
    import java.util.concurrent.TimeUnit;

    /**
     * @author pnoker
     */
    @Slf4j
    @Service("virtualDriverCustomService")
    public class DriverCustomServiceImpl implements DriverCustomService {


        @Autowired
        private ContainerContext containerContext;

        @Resource
        private DriverService driverService;


        @Override
        public void initial() {
        }

        @Override
        public String read(Map<String, AttributeInfo> driverInfo, Map<String, AttributeInfo> pointInfo, Device device, Point point) throws Exception {
            if (point.getType().equals("string")) {
                return RandomUtil.randomString(8);
            }
            if (point.getType().equals("boolean")) {
                return String.valueOf(RandomUtil.randomBoolean());
            }
            return String.valueOf(RandomUtil.randomDouble(100));
        }

        @Override
        public Boolean write(Map<String, AttributeInfo> driverInfo, Map<String, AttributeInfo> pointInfo, Device device, AttributeInfo value) throws Exception {
            return false;
        }

        @Override
        public void schedule(String driverName) {

            /*
            TODO:设备状态
            上传设备状态，可自行灵活拓展，不一定非要在schedule()接口中实现，也可以在read中实现设备状态的设置；
            你可以通过某种判断机制确定设备的状态，然后通过driverService.deviceEventSender接口将设备状态交给SDK管理。

            设备状态（DeviceStatus）如下：
            ONLINE:在线
            OFFLINE:离线
            MAINTAIN:维护
            FAULT:故障
             */
            containerContext.factoryDriverContext(driverName)
                    .getDriverMetadata()
                    .getDeviceMap()
                    .keySet()
                    .forEach(id -> driverService.deviceEventSender(id, CommonConstant.Device.Event.HEARTBEAT, CommonConstant.Status.ONLINE, 25, TimeUnit.SECONDS));



        }

    }
